package top.sjydzq.www.controller.Mall;

import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import top.sjydzq.www.POJO.POJOUser;
import top.sjydzq.www.bean.Order;
import top.sjydzq.www.bean.Product;
import top.sjydzq.www.bean.User;
import top.sjydzq.www.service.OrderService;
import top.sjydzq.www.service.StarService;
import top.sjydzq.www.service.UserService;
import java.util.List;

@Controller
@RequestMapping("/user/")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private StarService starService;

    @RequestMapping("register")
    public String register() {
        return "Mall/User/register";
    }

    @RequestMapping(value = "handleRegister", method = RequestMethod.POST)
    public String handleRegister(POJOUser user, Model model, HttpSession session) {
        boolean status = true;
        String error = "";
        if (user.getEmail().equals("")) {
            status = false;
            error += "email地址不能为空！";
        }
        if (user.getPassword().equals("")) {
            status = false;
            error += "密码不能为空！";
        }
        if (user.getConfirm().equals("")) {
            status = false;
            error += "确认密码不能为空！";
        }
        if (user.getCaptcha().equals("")) {
            status = false;
            error += "验证码不能为空！";
        }
        if (!user.getPassword().equals(user.getConfirm())) {
            status = false;
            error += "两次密码不一样！";
        }
        if (!user.getCaptcha().equals(session.getAttribute("captcha"))) {
            status = false;
            error += "验证码不正确！";
        }

        if (status) {
            if (userService.selectByEmail(user.getEmail()) != null) {
                status = false;
                error += "此邮箱已被注册！";
            } else {
                status = userService.addUser(user);
            }
        }

        model.addAttribute("status", status);
        model.addAttribute("error", error);

        return "Mall/User/result";
    }

    @RequestMapping("login")
    public String login() {
        return "Mall/User/login";
    }

    @RequestMapping(value = "handleLogin", method = RequestMethod.POST)
    public String handleLogin(POJOUser pojoUser, Model model, HttpSession session) {
        User user = userService.selectByEmail(pojoUser.getEmail());

         if (user == null) {
            model.addAttribute("status", false);
            model.addAttribute("error", "此邮箱未注册！");

            return "Mall/User/result";
        } else if (!pojoUser.getCaptcha().equals(session.getAttribute("captcha"))) {
             model.addAttribute("status", false);
             model.addAttribute("error", "验证码不正确！");

             return "Mall/User/result";
         } else {
             if (user.getBpwd().equals(pojoUser.getPassword())) {
                 session.setAttribute("user", user);

                 return "redirect:/";
             } else {
                 return "Mall/User/login";
             }
         }
    }

    @RequestMapping("userCenter")
    public String userCenterOrder(int uid,  Model model) {
        List<Order> orders = orderService.selectAllByUid(uid);
        List<Product> products = starService.selectAllByUid(uid);

        model.addAttribute("products",products);
        model.addAttribute("orders", orders);

        return "Mall/User/userCenter";
    }


    @RequestMapping("logout")
    public String handleLogout(HttpSession session) {
        session.removeAttribute("user");

        return "redirect:/";
    }
}
